<template>
  <div class="question-manage">
    <!-- 顶部操作栏 -->
    <el-card class="operation-card">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input
            v-model="searchQuery"
            placeholder="搜索试题"
            clearable
            @clear="handleSearch"
            @keyup.enter="handleSearch"
          >
            <template #append>
              <el-button @click="handleSearch">
                <el-icon><Search /></el-icon>
              </el-button>
            </template>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-select v-model="filterType" placeholder="题目类型" clearable>
            <el-option
              v-for="item in questionTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-select v-model="filterDifficulty" placeholder="难度等级" clearable>
            <el-option
              v-for="item in difficultyLevels"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-col>
        <el-col :span="10" style="text-align: right">
          <el-button type="primary" @click="handleAdd">
            新增试题
          </el-button>
          <el-button type="success" @click="handleImport">
            批量导入
          </el-button>
          <el-button type="warning" @click="handleExport">
            导出试题
          </el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 试题列表 -->
    <el-card class="list-card">
      <el-table :data="questionList" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="title" label="题目" show-overflow-tooltip />
        <el-table-column prop="type" label="类型" width="100">
          <template #default="scope">
            <el-tag :type="getQuestionTypeTag(scope.row.type)">
              {{ scope.row.type }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="difficulty" label="难度" width="100">
          <template #default="scope">
            <el-rate
              v-model="scope.row.difficulty"
              disabled
              show-score
              text-color="#ff9900"
            />
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button link type="primary" @click="handleEdit(scope.row)">
              编辑
            </el-button>
            <el-button link type="primary" @click="handlePreview(scope.row)">
              预览
            </el-button>
            <el-button link type="danger" @click="handleDelete(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 试题编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '新增试题' : '编辑试题'"
      width="60%"
    >
      <el-form :model="questionForm" label-width="100px">
        <el-form-item label="题目类型">
          <el-select v-model="questionForm.type">
            <el-option
              v-for="item in questionTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="题目">
          <el-input
            v-model="questionForm.title"
            type="textarea"
            :rows="3"
            placeholder="请输入题目内容"
          />
        </el-form-item>

        <el-form-item label="难度">
          <el-rate
            v-model="questionForm.difficulty"
            show-score
            text-color="#ff9900"
          />
        </el-form-item>

        <template v-if="questionForm.type === '选择题'">
          <el-form-item
            v-for="(option, index) in questionForm.options"
            :key="index"
            :label="`选项${String.fromCharCode(65 + index)}`"
          >
            <el-input v-model="questionForm.options[index]">
              <template #append>
                <el-button @click="removeOption(index)" :disabled="questionForm.options.length <= 2">
                  <el-icon><Delete /></el-icon>
                </el-button>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="addOption" :disabled="questionForm.options.length >= 6">
              添加选项
            </el-button>
          </el-form-item>
          <el-form-item label="正确答案">
            <el-select v-model="questionForm.answer">
              <el-option
                v-for="(_, index) in questionForm.options"
                :key="index"
                :label="String.fromCharCode(65 + index)"
                :value="String.fromCharCode(65 + index)"
              />
            </el-select>
          </el-form-item>
        </template>

        <template v-else-if="questionForm.type === '填空题'">
          <el-form-item label="参考答案">
            <el-input
              v-model="questionForm.answer"
              type="textarea"
              :rows="2"
              placeholder="请输入参考答案"
            />
          </el-form-item>
        </template>

        <template v-else-if="questionForm.type === '编程题'">
          <el-form-item label="参考答案">
            <el-input
              v-model="questionForm.answer"
              type="textarea"
              :rows="10"
              placeholder="请输入参考代码"
            />
          </el-form-item>
        </template>

        <el-form-item label="解析">
          <el-input
            v-model="questionForm.explanation"
            type="textarea"
            :rows="3"
            placeholder="请输入题目解析"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave">
            保存
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search, Delete } from '@element-plus/icons-vue'

// 题目类型选项
const questionTypes = [
  { label: '选择题', value: '选择题' },
  { label: '填空题', value: '填空题' },
  { label: '编程题', value: '编程题' }
]

// 难度等级选项
const difficultyLevels = [
  { label: '简单', value: 1 },
  { label: '中等', value: 2 },
  { label: '困难', value: 3 }
]

// 搜索和筛选
const searchQuery = ref('')
const filterType = ref('')
const filterDifficulty = ref('')

// 分页
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 对话框控制
const dialogVisible = ref(false)
const dialogType = ref('add')

// 试题表单
const questionForm = reactive({
  type: '选择题',
  title: '',
  difficulty: 1,
  options: ['', ''],
  answer: '',
  explanation: ''
})

// 模拟试题数据
const questionList = ref([
  {
    id: 1,
    title: '以下哪个不是C语言的基本数据类型？',
    type: '选择题',
    difficulty: 2,
    createTime: '2024-04-01 10:00:00'
  },
  {
    id: 2,
    title: '请实现一个冒泡排序算法',
    type: '编程题',
    difficulty: 3,
    createTime: '2024-04-01 11:00:00'
  }
])

// 获取题目类型对应的标签类型
const getQuestionTypeTag = (type) => {
  const typeMap = {
    '选择题': '',
    '填空题': 'success',
    '编程题': 'warning'
  }
  return typeMap[type]
}

// 搜索处理
const handleSearch = () => {
  // 实现搜索逻辑
}

// 分页处理
const handleSizeChange = (val) => {
  pageSize.value = val
  // 重新加载数据
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // 重新加载数据
}

// 新增试题
const handleAdd = () => {
  dialogType.value = 'add'
  Object.assign(questionForm, {
    type: '选择题',
    title: '',
    difficulty: 1,
    options: ['', ''],
    answer: '',
    explanation: ''
  })
  dialogVisible.value = true
}

// 编辑试题
const handleEdit = (row) => {
  dialogType.value = 'edit'
  Object.assign(questionForm, row)
  dialogVisible.value = true
}

// 删除试题
const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm('确定要删除这道题目吗？', '提示', {
      type: 'warning'
    })
    // 实现删除逻辑
    ElMessage.success('删除成功')
  } catch {
    // 用户取消删除
  }
}

// 导入试题
const handleImport = () => {
  // 实现导入逻辑
}

// 导出试题
const handleExport = () => {
  // 实现导出逻辑
}

// 选择题选项操作
const addOption = () => {
  questionForm.options.push('')
}

const removeOption = (index) => {
  questionForm.options.splice(index, 1)
}

// 保存试题
const handleSave = () => {
  // 实现保存逻辑
  ElMessage.success(dialogType.value === 'add' ? '添加成功' : '更新成功')
  dialogVisible.value = false
}
</script>

<style scoped>
.question-manage {
  padding: 20px;
}

.operation-card {
  margin-bottom: 20px;
}

.list-card {
  margin-top: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style> 